var express = require('express');
var router = express.Router();
var reqUtil = require('../lib/req');
var LINK = require('../api/link');

router.get('/', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {

        reqUtil.getData(LINK.REQ_GET_USER_INFO + req.session.uid, function(code, data) {

            if (!code) {
                var obj = {};
                obj.user = user;

                if (data) {
                    obj.uid = data.user_id;
                    obj.nickname = data.user_nickname;
                    obj.sign = data.user_sign;
                    obj.realname = data.user_realname;
                    obj.sex = data.gender;
                    obj.mobile = data.user_phone;
                    obj.email = data.user_email;
                    obj.desc = data.about;
                }
                res.render('info/user', obj);
            } else {
                res.redirect('/');
            }
        });

    } else {
        res.render('login', { link: req.originalUrl });
    }
});

router.get('/address', function(req, res) {

    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        reqUtil.getData(LINK.REQ_GET_CITY, function(code, data) {
            if (!code) {
                var obj = {
                    _city: JSON.stringify(data)
                };
                console.log(req.session.uid);
                reqUtil.getData(LINK.REQ_GET_USER_ADDRESS + encodeURIComponent(req.session.uid), function(code, data) {
                    console.log(data);
                    if (!code) {
                        obj.user = user;
                        if (data) {
                            obj.province = data.am_id_1;
                            obj.city = data.am_id_2;
                            obj.zone = data.am_id_3;
                            obj.addressee = data.usa_user_realname;
                            obj.mobile = data.usa_user_phone;
                            obj.address = data.usa_address;
                        }
                        res.render('info/address', obj);
                    } else {
                        res.redirect('/');
                    }
                });
            } else {
                res.redirect('/');
            }
        });
    } else {
        res.render('login', { link: req.originalUrl });
    }
});

router.get('/pwd', function(req, res) {

    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        res.render('info/chpwd', { user: user });
    } else {
        res.render('login', { link: req.originalUrl });
    }
});

router.get('/school', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        reqUtil.getData(LINK.REQ_GET_USER_INFO + req.session.uid, function(code, data) {
            if (!code) {
                reqUtil.getData(LINK.REQ_GET_CITY, function(code, city) {
                    if (!code) {
                        var obj = {
                            _city: JSON.stringify(city),
                            user: user
                        };
                        if (data) {
                            obj.province = data.am_id_1;
                            obj.city = data.am_id_2;
                            obj.zone = data.am_id_3;
                            obj.school = data.sm_id;
                            obj.cls = data.class_num;
                            obj.schoolNmae = data.sm_name;
                        }
                        console.log(obj);
                        res.render('info/school', obj);
                    } else {
                        res.redirect('/');
                    }
                });
            } else {
                res.redirect('/');
            }
        });

    } else {
        res.render('login', { link: req.originalUrl });
    }
});

router.get('/icon', function(req, res) {
    var obj = {};
    res.render('info/icon', obj);
});

router.post('/saveIcon', function(req, res) {
    var obj = {};
    res.render('info/icon', obj);
});

router.post('/savePassword', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var old = req.body.oldpass;
        var n = req.body.newpass;
        var confirm = req.body.confirmpass;
        if (n != confirm) {
            res.json({ code: 1, result: '新密码输入不一致！' });
        } else {
            reqUtil.postData(LINK.REQ_POST_USER_PASSWORD, { token: req.session.uid, user_main: { user_password_old: old, user_password_new: n } }, function(code, data) {
                if (!code) {
                    req.session.uid = null;
                    req.session.user = null;
                    res.json({
                        code: 0,
                        result: ''
                    });
                } else {
                    res.json({ code: code, result: data });
                }
            });
        }

    } else {
        res.render('login', { link: req.originalUrl });
    }

});

router.post('/saveSchool', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var info = {};
        var obj = req.body;
        info.am_id_1 = obj.province;
        info.am_id_2 = obj.city;
        info.am_id_3 = obj.zone;
        info.sm_id = obj.school;
        info.class_id = obj.cls;
        console.log(obj, info);
        reqUtil.postData(LINK.REQ_POST_USER_INFO, { token: req.session.uid, user_detail: info }, function(code, data) {
            if (!code) {
                res.json({
                    code: 0,
                    result: ''
                });
            } else {
                res.json({ code: code, result: data });
            }
        });

    } else {
        res.render('login', { link: req.originalUrl });
    }

});

router.post('/saveAddress', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var obj = req.body;
        var info = {};
        info.am_id_1 = obj.province;
        info.am_id_2 = obj.city;
        info.am_id_3 = obj.zone;

        info.usa_user_phone = obj.mobile;
        info.usa_user_realname = obj.addressee;
        info.usa_address = obj.address;

        reqUtil.postData(LINK.REQ_POST_USER_ADDRESS, { token: req.session.uid, user_shipping_address: info }, function(code, data) {
            if (!code) {
                res.json({
                    code: 0,
                    result: ''
                });
            } else {
                res.json({ code: code, result: data });
            }
        });

    } else {
        res.render('login', { link: req.originalUrl });
    }


});

router.post('/saveBasic', function(req, res) {
    var user = false;
    if (req.session) {
        if (req.session.user) {
            user = true;
        }
    }
    if (user) {
        var info = {};
        var obj = req.body;
        info.user_nickname = obj.nickname;
        info.user_sign = obj.sign;
        info.user_realname = obj.realname;
        info.gender = obj.sex;
        info.user_phone = obj.mobile;
        info.user_email = obj.email;
        info.about = obj.desc;
        reqUtil.postData(LINK.REQ_POST_USER_INFO, { token: req.session.uid, user_detail: info }, function(code, data) {
            if (!code) {
                res.json({
                    code: 0,
                    result: ''
                });
            } else {
                res.json({ code: code, result: data });
            }
        });

    } else {
        res.render('login', { link: req.originalUrl });
    }

});

module.exports = router;